A portion of this patent document contains material that is subject to copyright protection. The copyright owner has no objection to the facsimile reproduction by anyone of the patent document or the patent disclosure, as it appears in the Patent and Trademark Office patent file or records, but otherwise reserves all copyright rights whatsoever.
The present invention generally relates to property lists implemented in object-oriented programming languages, and more particularly to an implementation of a heterogeneous property list that is type-safe.
A property list is a list of attributed value pairs, where each pair associates a key (the attribute) with a value. In a heterogeneous property list, the values are of different types.
A property list is a LISP concept, and is useful for attaching information to objects or symbols. In extended Backus-Naur Form a property list may be defined as:
There are Put and Get functions associated with a property list. A property is inserted in a list by providing a property name and property value as input to the Put function:
Put(propertylist, propname, propvalue)
Similarly, a property value is retrieved by providing the property name that identifies the value as input to the Get function:
value=Get(propertylist,propname)
A property list may be included as a data member of a class and used to store many types of information with regard to objects of the class. Property lists are often used when it would be too expensive to add a regular data member for the information. A property list may be more desirable than a regular data member when only a few objects of a given class would ever need the information, or if the information is only needed temporarily.
An example where only a few objects of a given class would ever need the information is where all objects of a class have a default value and only a few objects need to overwrite the default value. Consider the employees of a United States company where 99% are U.S. citizens. Rather than storing citizenship for all employees using a regular data member, a property list can instead be used to indicate when an employee""s citizenship is other than U.S. A function that gets the citizenship of an employee checks the property list for the presence of a citizenship property value. If a citizenship property value is present, that value is returned, otherwise the value xe2x80x9cU.S.xe2x80x9d is returned.
An example where the information is only needed temporarily is a verification phase that converts all objects of a class into a canonical form. After conversion to canonical form, the objects are sorted and duplicate objects are removed. Once the duplicate objects have been removed, the canonical form is no longer needed and can be discarded.
A disadvantage of using a property list over a regular data member is that the property list itself requires additional memory. A property value is always associated with a property name, while only the property value is needed if implemented as a regular data member. Another disadvantage is that inserting and retrieving a property value involves searching the list for a matching property name. The additional memory implies that a property list should be used only when there is a need for recording more than a few properties. Once the property list has been included as a data member of a class, a property should be used instead of a regular data member only if the property is short lived or recorded with only a few objects, and the extra overhead in accessing the property value is relatively unimportant. It will be appreciated that the overhead in inserting and retrieving a property can be reduced by implementing the list as a hash table, for example.
A heterogeneous property list is an associative container capable of storing objects or pointers to objects of different types. For example, a heterogeneous property list for an employee object might include a salary property having a real number value and a citizenship property having a character string value.
A property list can be created to store the data as a generic pointer, leaving to user programs the requirement to cast the data to the proper type when retrieving it. However, the disadvantage of this approach is that the data are not type safe. That is, data corruption may occur if the user program does not cast the data to the appropriate type. Furthermore, only pointers to the objects can be stored rather than the objects themselves. A method that address the aforementioned problems, as well as other related problems, is therefore desirable.
A method and an apparatus for implementing type-safe, heterogeneous object-oriented property lists are provided in various embodiments of the invention. In one embodiment, a library of classes and template classes implementing a heterogeneous property list is defined, wherein each object of the property list includes a property name and an associated property value of a selected property type. Instantiation of objects having equal property names and unequal property types is prevented with a constructor, and template functions are provided each of which accesses the objects of the property list according to a type of data passed to the function.
In another method for implementing type-safe property lists, a first class having a property name and a property type is defined. A second class is defined as having a pointer to an object of the first class and a constructor that aborts a program instantiating an object of the second class if, for another object of the first class having a property name and property type, an input property name matches the property name of the other object and an input property type does not match the property type of the other object. A third class is defined as having a member that is an object of the second class. A first template class is defined as having a constructor that passes a property name and property type to the constructor of the second class. A second template class is defined as having a constructor that passes an object of the first template class to the third class, a property value member, and one or more member functions for accessing the property value member. A property list class is defined as having template functions that get and put objects of the second template class in a property list object, wherein the template functions have property type and property value inputs. Selected objects of the second template class are used to instantiate a property list object.
In another embodiment, an apparatus is provided for implementing object-oriented property lists. The apparatus comprises: means for defining a library of classes and template classes implementing a heterogeneous property list, wherein each object of the property list includes a property name and an associated property value of a selected property type; means for providing a constructor that prevents instantiation of objects having equal property names and unequal property types; and means for providing template functions each of which accesses the objects of the property list according to a type of data passed to the function.
The above summary of the present invention is not intended describe each disclosed embodiment of the present invention. The figures and detailed description that follow provide additional example embodiments and aspects of the present invention.